package com.zzyl.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.pojo.PageResult;
import com.zzyl.pojo.Reservation;
import com.zzyl.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
public class ReservationController {
    @Autowired
    ReservationMapper reservationMapper;
    //新增预约
    @PostMapping("/customer/reservation")
    public Result addReservation(@RequestBody HashMap<String,Object> params){
        //补全数据
        //1.补全状态
        params.put("status",0);
        //2.补全创建时间
        params.put("createTime", LocalDateTime.now());
        //3.补全更新时间
        params.put("updateTime",LocalDateTime.now());

        //调用mapper完成添加
        reservationMapper.insert(params);
        return Result.success();
    }

    @GetMapping("/customer/reservation/page")
    public Result page(Integer userId, Integer status, Integer pageNum, Integer pageSize) {
        log.info("userId:{},status:{}",userId, status);
        log.info("pageNum:{},pageSize:{}",pageNum,pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<Reservation> reservations = reservationMapper.findPage(userId,status);
        //1. 设置分页参数

        //2. 查询数据获取分页数据
        Page<Reservation> p=(Page<Reservation>)reservations;
        //3. 创建分页结果对象
        PageResult<Reservation> pr = new PageResult<>();
        //4. 设置分页结果对象
        pr.setPage(pageNum);
        pr.setPages(Long.valueOf(p.getPages()));
        pr.setPageSize(pageSize);
        pr.setTotal(p.getTotal());
        pr.setRecords(p.getResult());
        //5. 响应结果对象
        return Result.success(pr);
    }
    @PutMapping("/customer/reservation/{id}/cancel")
    public Result cancel(@PathVariable Integer id,Integer userId){
        HashMap<String, Object>params = new HashMap<>();
        params.put("id",id);
        params.put("userId",userId);
        params.put("updateTime",LocalDateTime.now());
        reservationMapper.updateStatus(params);
        return Result.success();

    }

}










